/*
 * Main User class
 * 
 * 
 */
package edu.gatech.volt2.drdoommgt.domain;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

/**
 *
 * @author James Rundquist
 */

@Entity
@Table(name="users")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class User{
	
	@Id
	@GeneratedValue(generator="increment")
	@GenericGenerator(name="increment", strategy = "increment")
	private long id;				// Unique id of the user
	
	private String email;		// Email for each user
	private String password;	// Hashed password of the user
	
	@Embedded
	private Name name;			// Name of the user

	private UserType type;		// Type the user is [ Enum type ]
	private boolean suspended;	// Whether or not they are suspended from the system
	private boolean loggedIn;	// Whether or not they are logged into the system currently
	
	
	private static Logger myLogger = Logger.getLogger("edu.gatech.volt2.drdoommgt.domain.User");

	
	/**
	 * Blank constructor
	 */
	public User(){
		// Grant as few permissions as possible with the default user
//		this.type = UserType.PATIENT;
		// In the future we may need to set something that tells the system this user is an empty object
	}
	
	
	/**
	 * Create a user object from the passed id
	 */
	public User(int id){
		// :TODO: Get the user from the database with this id and populate the proper fields
	}
	
	
	/**
	 * Create a user object from the passed name
	 */
	public User(Name name){
		this.setName(name);
	}
	
	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}


	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}


	public String getPassword(){
		return this.password;
	}
	
	public void setPassword(String pass){
		// :TODO: pass changes to the database
		
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("SHA-256");
			
			messageDigest.update(pass.getBytes());
			 
	        byte byteData[] = messageDigest.digest();
	 
	        //convert the byte returned from the hash method into a string
	        StringBuffer sb = new StringBuffer();
	        for (int i = 0; i < byteData.length; i++) {
	         sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
	        }
	        this.password = sb.toString();
	        
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
            myLogger.log(Level.SEVERE, "No such Algorithm 'SHA-256'", e);

			this.password = pass;
			System.err.println("No such Algorithm 'SHA-256' in User.setPassword()");
		}
	}

	
	/**
	 * Call to log the user out of the system 
	 */
	public void logOut(){
		// :TODO: Make a call to whatever authentication manager we set up
	}


	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}
	
	/**
	 * @param type the type to set
	 */
	public void setType(UserType type) {
		this.type = type;
	}


	/**
	 * @return the type
	 */
	public UserType getType() {
		return type;
	}


	/**
	 * @param name the name to set
	 */
	public void setName(Name name) {
		this.name = name;
	}


	/**
	 * @return the name
	 */
	public Name getName() {
		return name;
	}


	/**
	 * @param loggedIn the loggedIn to set
	 */
	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}


	/**
	 * @return the loggedIn
	 */
	public boolean isLoggedIn() {
		return loggedIn;
	}


	/**
	 * @param suspended the suspended to set
	 */
	public void setSuspended(boolean suspended) {
		this.suspended = suspended;
	}


	/**
	 * @return the suspended
	 */
	public boolean isSuspended() {
		return suspended;
	}

	public String toString(){
		return ""+this.id+":"+/*this.type+*/":"+this.name.toString();
	}
	
}
